package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.common.R;
import org.example.entity.Business;
import org.example.entity.Employee;
import org.example.entity.User;
import org.example.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * TODO 商家
 *
 * @author shaoxuan.
 * @date 2023/12/23 16:39
 */
@RestController
@RequestMapping("/business")
@Api(tags = "商家管理")

@Slf4j
public class BusinessController {

    @Autowired
    private BusinessService service;

    /**
     * 商家注册
     *
     * @param business
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "商家注册")

    public R register(@RequestBody Business business) {
        String password = business.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        Business one = service.getOne(new LambdaQueryWrapper<Business>().
                eq(Business::getUsername, business.getUsername()));
        if (one == null) {
            business.setPassword(password);
            service.save(business);
            return R.success("注册成功");
        } else {
            return R.error("用户名已存在，注册失败");
        }


    }

    /**
     * 商家登录
     *
     * @param request
     * @param business
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "商家登录")

    public R<Business> login(HttpServletRequest request, @RequestBody Business business) {
        //1、将页面提交的密码password进行md5加密处理
        String password = business.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Business::getUsername, business.getUsername());
        Business emp = service.getOne(queryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if (emp == null) {
            return R.error("登录失败");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }

        //6、登录成功，将店家id存入Session并返回登录成功结果
        request.getSession().setAttribute("business", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前登录用户的id
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }


    /**
     * 根据id获取店家信息
     *
     * @param businessId
     * @return
     */
    @GetMapping("/getInfo/{businessId}")
    @ApiOperation(value = "根据id获取店家信息")

    public R<Business> getInfoBy(@PathVariable("businessId") String businessId) {
        Business one = service.getOne(new LambdaQueryWrapper<Business>().eq(Business::getId, businessId));
        return R.success(one);
    }

    /**
     * 修改店家信息
     *
     * @param business
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改店家信息")

    public R<String> update(@RequestBody Business business) {
        if (business.getId() != null) {
            service.updateById(business);
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * 获取所有商家
     * @return
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "获取所有商家")

    public R<List<Business>> getAll(){
        List<Business> list = service.list();
        return R.success(list);
    }
}
